package com.groupten.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.groupten.Dao.WqHouseDao;
import com.groupten.Entity.QuestionEntity;
import com.groupten.Entity.WqHouseEntity;
import com.groupten.Service.QuestionService;
import com.groupten.Service.WqHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.server.LogStream;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("wqHouseService")
public class WqHouseServiceImpl extends ServiceImpl<WqHouseDao, WqHouseEntity> implements WqHouseService {

    @Autowired
    private QuestionService questionService;

    @Override
    public Integer insertWqHouse(WqHouseEntity wqHouse) {
        List<WqHouseEntity> wqHouseEntityList = selectWqHouseByStudentNumberAndQuestionId(wqHouse.getStudentNumber(), wqHouse.getQuestionId(), null, null).getRecords();
        if (wqHouseEntityList.isEmpty()){
            //对于这个学生，错题库里没有这个错题
            return baseMapper.insert(wqHouse);
        }
        return 0;
    }

    @Override
    public Integer deleteWqHouseById(Long Id) {
        LambdaQueryWrapper<WqHouseEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(Id),WqHouseEntity::getHouseId,Id);

        return baseMapper.delete(queryWrapper);
    }

    @Override
    public IPage<WqHouseEntity> selectAll(Integer page, Integer limit) {
        int curPage = 1;
        int curLimit = 10;
        if (ObjectUtils.isNotEmpty(page)) {
            curPage = Integer.parseInt(page.toString());
        }
        if (ObjectUtils.isNotEmpty(limit)) {
            curLimit = Integer.parseInt(limit.toString());
        }

        IPage<WqHouseEntity> pageObject = Page.of(curPage, curLimit);

        return baseMapper.selectPage(pageObject, Wrappers.lambdaQuery());
    }


    @Override
    public WqHouseEntity selectWqHouseByQuestionId(Integer QuestionId) {
        LambdaQueryWrapper<WqHouseEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(QuestionId),WqHouseEntity::getQuestionId,QuestionId);
        
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public IPage<WqHouseEntity> selectWqHouseBySubject(Integer Subject, Integer page, Integer limit) {
        LambdaQueryWrapper<WqHouseEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(Subject),WqHouseEntity::getSubject,Subject);

        int curPage = 1;
        int curLimit = 10;
        if (ObjectUtils.isNotEmpty(page)) {
            curPage = Integer.parseInt(page.toString());
        }
        if (ObjectUtils.isNotEmpty(limit)) {
            curLimit = Integer.parseInt(limit.toString());
        }

        IPage<WqHouseEntity> pageObject = Page.of(curPage, curLimit);

        return baseMapper.selectPage(pageObject, queryWrapper);
    }

    @Override
    public IPage<WqHouseEntity> selectWqHouseByKnowledge(Integer KnowledgePoint, Integer page, Integer limit) {
        LambdaQueryWrapper<WqHouseEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(KnowledgePoint),WqHouseEntity::getKnowledgePoint,KnowledgePoint);

        int curPage = 1;
        int curLimit = 10;
        if (ObjectUtils.isNotEmpty(page)) {
            curPage = Integer.parseInt(page.toString());
        }
        if (ObjectUtils.isNotEmpty(limit)) {
            curLimit = Integer.parseInt(limit.toString());
        }

        IPage<WqHouseEntity> pageObject = Page.of(curPage, curLimit);

        return baseMapper.selectPage(pageObject, queryWrapper);
    }

    @Override
    public IPage<WqHouseEntity> selectWqHouseByQuestionType(Integer QuestionType, Integer page, Integer limit) {
        LambdaQueryWrapper<WqHouseEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ObjectUtils.isNotEmpty(QuestionType),WqHouseEntity::getQuestionType,QuestionType);

        int curPage = 1;
        int curLimit = 10;
        if (ObjectUtils.isNotEmpty(page)) {
            curPage = Integer.parseInt(page.toString());
        }
        if (ObjectUtils.isNotEmpty(limit)) {
            curLimit = Integer.parseInt(limit.toString());
        }

        IPage<WqHouseEntity> pageObject = Page.of(curPage, curLimit);

        return baseMapper.selectPage(pageObject,queryWrapper);
    }

    @Override
    public IPage<WqHouseEntity> selectWqHouseByStudentNumber(String StudentNumber, Integer page, Integer limit) {
        LambdaQueryWrapper<WqHouseEntity> queryWrapper = new LambdaQueryWrapper<WqHouseEntity>()
                .eq(StudentNumber!=null && !StudentNumber.isEmpty(), WqHouseEntity::getStudentNumber, StudentNumber);

        int curPage = 1;
        int curLimit = 10;
        if (page!=null && page>0){
            curPage = page;
        }
        if (limit!=null && limit>0){
            curLimit = limit;
        }
        return baseMapper.selectPage(new Page<WqHouseEntity>(curPage, curLimit), queryWrapper);

    }

    @Override
    public IPage<WqHouseEntity> selectWqHouseByStudentNumberAndSubject(String studentNumber, Integer subject, Integer page, Integer limit) {
        LambdaQueryWrapper<WqHouseEntity> queryWrapper = new LambdaQueryWrapper<WqHouseEntity>()
                .eq(studentNumber!=null && !studentNumber.isEmpty(), WqHouseEntity::getStudentNumber, studentNumber)
                .eq(subject!=null && subject>0, WqHouseEntity::getSubject, subject);
        int curPage = 1;
        int curLimit= 10;
        if (page!=null && page>0){
            curPage = page;
        }
        if (limit!=null && limit>0){
            curLimit = limit;
        }
        return baseMapper.selectPage(new Page<WqHouseEntity>(curPage, curLimit), queryWrapper);
    }

    @Override
    public List<WqHouseEntity> selectWqHouseListByStudentNumberAndSubject(String studentNumber, Integer subject) {
        LambdaQueryWrapper<WqHouseEntity> queryWrapper = new LambdaQueryWrapper<WqHouseEntity>()
                .eq(studentNumber!=null && !studentNumber.isEmpty(), WqHouseEntity::getStudentNumber, studentNumber)
                .eq(subject!=null && subject>0, WqHouseEntity::getSubject, subject);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<WqHouseEntity> selectWqHouseByStudentNumberAndQuestionId(String studentNumber, Integer questionId, Integer page, Integer limit) {
        LambdaQueryWrapper<WqHouseEntity> queryWrapper = new LambdaQueryWrapper<WqHouseEntity>()
                .eq(studentNumber!=null && !studentNumber.isEmpty(), WqHouseEntity::getStudentNumber, studentNumber)
                .eq(questionId!=null && questionId>0, WqHouseEntity::getQuestionId, questionId);
        int curPage = 1;
        int curLimit= 10;
        if (page!=null && page>0){
            curPage = page;
        }
        if (limit!=null && limit>0){
            curLimit = limit;
        }
        return baseMapper.selectPage(new Page<WqHouseEntity>(curPage, curLimit), queryWrapper);
    }

    @Override
    public List<WqHouseEntity> selectWqHouseListByStudentNumberAndQuestionId(String studentNumber, Integer questionId) {
        LambdaQueryWrapper<WqHouseEntity> queryWrapper = new LambdaQueryWrapper<WqHouseEntity>()
                .eq(studentNumber!=null && !studentNumber.isEmpty(), WqHouseEntity::getStudentNumber, studentNumber)
                .eq(questionId!=null && questionId>0, WqHouseEntity::getQuestionId, questionId);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Integer deleteAll() {
        return baseMapper.delete(Wrappers.lambdaQuery());
    }

    @Override
    public void distinct() {
        List<WqHouseEntity> collect = baseMapper.selectList(Wrappers.lambdaQuery());
        List<WqHouseEntity> collect1 = collect.stream().distinct().collect(Collectors.toList());
        deleteAll();
        collect1.forEach(this::insertWqHouse);
    }

    @Override
    public Integer WQHouseIns(Map<Double, List<QuestionEntity>> answerResult, String studentID) {
        WqHouseEntity wrongQuestionHouse = new WqHouseEntity();
        List<QuestionEntity> wrongQuestions = new ArrayList<>();
        for(List<QuestionEntity> list:answerResult.values()) wrongQuestions=list;
        int ret=0;
        //录入全部错题数据
        for(QuestionEntity question:wrongQuestions) {
            setWrongQuestion(studentID, wrongQuestionHouse, question);  //封装类属性设定
            wrongQuestionHouse.setQuestionType(question.getQuestionType());
            ret = ret + this.insertWqHouse(wrongQuestionHouse);
        }
        return ret;
    }
    //设定wrongQuestion的各个属性
    public static void setWrongQuestion(String studentID, WqHouseEntity wrongQuestionHouse, QuestionEntity question) {
        wrongQuestionHouse.setScore(question.getScore());
        wrongQuestionHouse.setKnowledgePoint(question.getKnowledgePoint());
        wrongQuestionHouse.setLevel(question.getLevel());
        wrongQuestionHouse.setQuestionId(Integer.valueOf(String.valueOf(question.getQuestionId())));
        wrongQuestionHouse.setKnowledgePoint(question.getKnowledgePoint());
        wrongQuestionHouse.setSubject(question.getSubject());
        wrongQuestionHouse.setStudentNumber(studentID);
    }

    @Override
    public void deleteObsoleteQuestion() {
        List<WqHouseEntity> wqHouseList = baseMapper.selectList(Wrappers.lambdaQuery());
        for (WqHouseEntity wqHouse : wqHouseList){
            if (questionService.selectQuestionByID(Long.valueOf(wqHouse.getQuestionId()))==null){
                baseMapper.deleteById(wqHouse.getHouseId());
            }
        }
    }

}